const fs = require('fs');
const path = require("path");

function loadImage(name){
  let bitmap = fs.readFileSync(path.join(__dirname,`../img/${name}`));
  let base64str = Buffer.from(bitmap, 'binary').toString('base64'); // base64编码
  return 'data:image/png;base64,'+base64str
}
const def=loadImage("默认图.png")
const PId=[
  {
    "id":5,
    "name":"笔记本电脑",
    "type":"办公用品",
    "imp":"一般",
    "hum":[],
    img:def,
    "repo":[
      {
        "id":"2351",
        "rfid":"FWAEAE",
        "base":"一仓库",
        "last":"等待管理员入库"
      },      {
        "id":"2352",
        "rfid":"AVRNLI",
        "base":"一仓库",
        "last":"等待管理员入库"
      },    {
        "id":"2353",
        "rfid":"YSEOCM",
        "base":"一仓库",
        "last":"等待管理员入库"
      },   {
        "id":"2354",
        "rfid":"KDDALI",
        "base":"二仓库",
        "last":"等待管理员入库"
      },  {
        "id":"2355",
        "rfid":"CAICNE",
        "base":"一仓库",
        "last":"等待管理员入库"
      },{
        "id":"2356",
        "rfid":"JFJJSH",
        "base":"三仓库",
        "last":"等待管理员入库"
      },{
        "id":"2357",
        "rfid":"CLSNFJ",
        "base":"一仓库",
        "last":"等待管理员入库"
      },
    ],
  },{
    "id":6,
    "name":"手持RFID扫描仪",
    "type":"工作用品",
    "imp":"重要",
    hum:[],
    img:def,
    repo:[
      {
        "id":"2358",
        "rfid":"DAWFGW",
        "base":"一仓库",
        "last":"等待管理员入库"
      },
      {
        "id":"2359",
        "rfid":"WDSRGR",
        "base":"一仓库",
        "last":"等待管理员入库"
      },
      {
        "id":"2360",
        "rfid":"DAEGRC",
        "base":"一仓库",
        "last":"等待管理员入库"
      }
    ]
  },
  {
    "id":7,
    "name":"玻璃瓶",
    "type":"易碎物品",
    "imp":"非常重要",
    hum:[],
    img:def,
    repo:[
      {
        "id":"2361",
        "rfid":"GAWFWG",
        "base":"二仓库",
        "last":"等待管理员入库"
      },
      {
        "id":"2362",
        "rfid":"GGEAEG",
        "base":"二仓库",
        "last":"等待管理员入库"
      },
      {
        "id":"2363",
        "rfid":"GRSESD",
        "base":"二仓库",
        "last":"等待管理员入库"
      },
      {
        "id":"2364",
        "rfid":"VSDVSR",
        "base":"二仓库",
        "last":"等待管理员入库"
      }
    ],
  }
]
var deviceList=[
  {
    "id":0,
    "name":"钳子",
    "type":"日常工具",
    "imp":"重要",
    "repo":[
      {
        "id":"2351",
        "rfid":"12af3g4sa44",
        "base":"1仓库",
        "last":"2021/8/9 12:45"
      }
    ],
    "hum":[
      {
        "id":"1313",
        "rfid":"113516sefssg",
        "owner":"张三",
        "last":"2021/8/9 12:45"
      }
    ],
    "img":loadImage("天平.png")
  },
  {
    "id":1,
    "name":"支架",
    "type":"日常工具",
    "imp":"重要",
    "repo":[
    ],
    "hum":[
    ],
    "img":loadImage("支架.png")
  },
  {
    "id":2,
    "name":"示波器",
    "type":"日常工具",
    "imp":"重要",
    "repo":[
    ],
    "hum":[
    ],
    "img":loadImage("示波器.png")
  },
  {
    "id":3,
    "name":"演示仪",
    "type":"日常工具",
    "imp":"重要",
    "repo":[
    ],
    "hum":[
    ],
    "img":loadImage("演示仪.png")
  },
  {
    "id":4,
    "name":"烧杯",
    "type":"日常工具",
    "imp":"重要",
    "repo":[
    ],
    "hum":[
    ],
    "img":loadImage("烧杯.png")
  }
]
function findById(list,id){
  return list.find((item)=>{
    return item.id==id
  })
}
function getData(){
  let data=[]
  for(let i in deviceList){
    let n=deviceList[i]
    let d={
      "id":n.id,
      "name":n.name,
      "type":n.type,
      "imp":n.imp,
      "img":n.img,
      "in":n.repo.length,
      "num":n.hum.length+n.repo.length
    }
    data.push(d)
  }
  return data
}
module.exports=[
  {
    url: '/device/list',
    type: 'get',
    response: config => {

      return {
        code: 20000,
        data: getData()
      }
    }
  },
  {
    url: '/device/create',
    type: 'post',
    response: config => {
      const nd=config.body
      if(deviceList.length!=0)
        nd.id=deviceList[deviceList.length-1].id+1
      else
        nd.id=0
      nd.repo=[]
      nd.hum=[]
      deviceList.push(nd)
      return {
        code: 20000,
      }
    }
  },
  {
    url: '/device/update',
    type: 'post',
    response: config => {
      const nd=config.body
      const od=findById(deviceList,nd.id)
      od.name=nd.name
      od.type=nd.type
      od.imp=nd.imp
      od.img=nd.img
      return {
        code: 20000,
      }
    }
  },
  {
    url: '/device/delete',
    type: 'post',
    response: config => {
      const id=config.body.id
      const ind=deviceList.findIndex((item)=>{
        return item.id==id
      })
      if(ind!=-1)
        deviceList.splice(ind,1)
      return {
        code: 20000,
      }
    }
  },  {
    url: '/device/ins/add',
    type: 'post',
    response: config => {
      const d=config.body
      const dev=findById(deviceList,d.id)
      const rp=dev.repo
      let id=0
      rp.push({
        id:(Math.random()*10000).toFixed(0),
        rfid:d.rfid,
        base:d.base,
        "last":"等待管理员入库"
      })
      return {
        code: 20000,
      }
    }
  },  {
    url: '/device/ins/list',
    type: 'post',
    response: config => {
      const d=config.body
      const dev=findById(deviceList,d.id)
      return {
        data:dev,
        code: 20000,
      }
    }
  },
  {
    url: '/device/ps',
    type: 'post',
    response: config => {
      deviceList=deviceList.concat(PId)
      return {
        code: 20000,
      }
    }
  },
]


